package com.example.healthsystem.controller;


import com.example.healthsystem.entity.user;
import com.example.healthsystem.model.dto.LoginInfo;
import com.example.healthsystem.model.vo.Result;
import com.example.healthsystem.service.UserService;
import com.example.healthsystem.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

//@RestController
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(){
        // --- 登录页面
        return "/common/login";
    }

    @PostMapping("/login")
    public String login(Model model,
                        user user,
                        HttpServletRequest request){

        user requser=userService.findUserByUsernameAndPassword(user.getUsername(),
                user.getPassword());
        model.addAttribute("user",requser);

        if(requser==null){
            System.out.println("can't find the user");
            return "/common/login";
        }else if(requser.getRole().equals("ROLE_admin")){
            System.out.println("this is admin login");
            return "/admin/admin";
        }else if(requser.getRole().equals("ROLE_student")||requser.getRole().equals("ROLE_teacher")){
            if(requser.getSitu()==0){
                // --- 此时用户处于禁用状态
                return "/common/login";
            }
            System.out.println("this is user login");
            return "/user/user";
        }else {
            System.out.println("this is another situation");
            return "/common/login";
        }

    }
}
